package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "用户地址薄相关接口")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @GetMapping("/list")
    @ApiOperation("查询地址薄")
    public Result<List<AddressBook>> list(){
        Long currentId = BaseContext.getCurrentId();
        return Result.success(addressService.list(currentId));
    }

    @PostMapping
    @ApiOperation("新增地址")
    public Result add(@RequestBody AddressBook addressBook){
        addressService.add(addressBook);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设为默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressService.setDefault(addressBook.getId());
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        return Result.success(addressService.getById(id));
    }

    @PutMapping
    @ApiOperation("修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        addressService.update(addressBook);
        return Result.success();
    }

    @DeleteMapping()
    @ApiOperation("根据id删除地址")
    public Result delete(Long id) {
        addressService.delete(id);
        return Result.success();
    }


    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        return Result.success(addressService.getDefault());
    }








}
